
import request from '@/utils/request'
import OSS from 'ali-oss'
import MD5 from 'md5'
/**
 * 获取类型列表
 * params.pid 14新闻 15下载 16媒体
 */
export function getCateList(params) {
  return request({
    url: '/api/category/list',
    method: 'get',
    params
  })
}

/**
 * 获取列表
 */
export function getPageList(params) {
  return request({
    url: '/api/tpl/listAll',
    method: 'get',
    params
  })
}
/**
 * 获取列表
 */
export function getPageDetail(params) {
  return request({
    url: '/api/tpl/detail',
    method: 'get',
    params
  })
}
/**
 * 获取列表
 */
export function addPage(params) {
  return request({
    url: '/api/tpl/add',
    method: 'post',
    data: params
  })
}

export function editPage(params) {
  return request({
    url: '/api/tpl/edit',
    method: 'post',
    data: params
  })
}

export function delPage(params) {
  return request({
    url: '/api/tpl/del',
    method: 'post',
    data: params
  })
}


/**
 * 新增类型
 * params.name 名称
 * params.pid 14新闻 15下载 16媒体
 */
export function addCate(params) {
  return request({
    url: '/api/category/add',
    method: 'post',
    data: params
  })
}

/**
 * 新增下载中心
 */
export function addDownload(params) {
  return request({
    url: '/api/download/add',
    method: 'post',
    data: params
  })
}

/**
 * 新增下载中心
 */
export function addMedia(params) {
  return request({
    url: '/api/media/add',
    method: 'post',
    data: params
  })
}



export function getProductList(params) {
  return request({
    url: '/api/product/pageList',
    method: 'get',
    params
  })
}

export function postProduct(params) {
  return request({
    url: '/api/product/add',
    method: 'post',
    data: params
  })
}

export function getProduct(params) {
  return request({
    url: '/api/product/detail',
    method: 'get',
    params
  })
}
export function editProduct(params) {
  return request({
    url: '/api/product/edit',
    method: 'post',
    data: params
  })
}
export function delProduct(params) {
  return request({
    url: '/api/product/del',
    method: 'delete',
    params
  })
}

// 上传文件
export function uploads({
  file,
  onProgress,
  type,
  dirname
}) {
  // https://material-product.oss-cn-hongkong.aliyuncs.com/goods/coil/coil-des-s274-1.png
  return new Promise((resolve, reject) => {
    const obj = {
      endpoint: 'oss-cn-hongkong.aliyuncs.com',
      accessKeyId: 'LTAI5t6DHFfiu3riG72xAujG',
      accessKeySecret: 'FhBLKpChHCdr2kFoWJT8EXcoi0WTUL',
      bucket: 'material-product',
      secure: true
    }
    const client = new OSS(obj)
    const ext = file.name.split('.').pop()
    const str = MD5(file.name.split('.')[0] + new Date().getTime())
    const path = `${dirname}/${str.substring(0, 8)}-${new Date().getTime()}.${ext}`
    client.multipartUpload(path, file, {
      progress: async function (p, checkpoint) {
        console.log('p', p)
        onProgress && onProgress({
          percent: Math.floor(p * 100)
        })
      }
    }).then(function (result) {
      console.log('result', result)
      resolve(result.res.requestUrls[0])
    }).catch(e => {
      reject(e)
    })
  })
}
